using System;

using Microsoft.SharePoint;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.Helpers
{
    public class SPListItemHelper
    {
        #region Getters
        private static object Get(SPListItem item, string key)
        {
            object o = null;

            try
            {
                o = item[key];
            }
            catch { }
            return o;
        }

        private static object Get(SPListItem item, Guid key)
        {
            object o = null;

            try
            {
                o = item[key];
            }
            catch { }
            return o;
        }

        #region GetBoolean
        public static bool GetBoolean(SPListItem item, string key, bool defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetBooleanOrDefault(o.ToString(), defaultValue);
        }

        public static bool GetBoolean(SPListItem item, Guid key, bool defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetBooleanOrDefault(o.ToString(), defaultValue);
        } 
        #endregion // GetBoolean

        #region GetString
        public static string GetString(SPListItem item, Guid key, string defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetStringOrDefault(o.ToString(), defaultValue);
        }

        public static string GetString(SPListItem item, string key, string defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetStringOrDefault(o.ToString(), defaultValue);
        }

        public static string GetString(SPListItem item, object key, string defaultValue)
        {
            return GetString(item, key.ToString(), defaultValue);
        }

        public static string GetString(SPListItem item, Guid key)
        {
            object o = Get(item, key);

            if (o != null)
                return o.ToString();
            return null;
        }
        public static string GetString(SPListItem item, string key)
        {
            object o = Get(item, key);

            if (o != null)
                return o.ToString();
            return null;
        }
        public static string GetString(SPListItem item, object key)
        {
            return GetString(item, key.ToString());
        }
        #endregion // GetString

        #region GetInteger
        public static int GetInteger(SPListItem item, string key, int defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetIntegerOrDefault(o.ToString(), defaultValue);
        }

        #endregion // GetInteger

        #region GetDateTime
        public static DateTime GetDateTime(SPListItem item, string key, DateTime defaultValue)
        {
            object o = Get(item, key);

            if (o == null)
                return defaultValue;
            else
                return Helper.GetDateTimeOrDefault(o.ToString(), defaultValue);
        }

        public static DateTime GetDateTime(SPListItem item, object key, DateTime defaultValue)
        {
            return GetDateTime(item, key.ToString(), defaultValue);
        }
        #endregion // GetDateTime

        #region GetUser
        public static SPUser GetUser(SPListItem item, string key, SPWeb web)
        {
            string value = GetString(item, key);

            if (string.IsNullOrEmpty(value))
                return null;
            else
            {
                SPFieldUserValue v = new SPFieldUserValue(web, value);
                return v.User;
            }
        }
        public static SPUser GetUser(SPListItem item, object key, SPWeb web)
        {
            string value = GetString(item, key);

            if (string.IsNullOrEmpty(value))
                return null;
            else
            {
                SPFieldUserValue v = new SPFieldUserValue(web, value);
                return v.User;
            }
        }
        #endregion // GetUser

        #region GetFieldValue
        public static string GetFieldTextValue(SPListItem item, string key)
        {
            object o = Get(item, key);

            if(o != null)
            {
                SPField field = GetField(item, key);
                return field.GetFieldValueAsText(o);
            }
            return string.Empty;
        }
        public static string GetFieldTextValue(SPListItem item, Guid key)
        {
            object o = Get(item, key);

            if (o != null)
            {
                SPField field = GetField(item, key);
                return field.GetFieldValueAsText(o);
            }
            return string.Empty;
        }
        #endregion // GetFieldValue


        #region GetField
        public static SPField GetField(SPListItem item, Guid key)
        {
            SPField field = null;

            try
            {
                field = item.Fields[key];
            }
            catch { }
            return field;
        }

        public static SPField GetField(SPListItem item, string key)
        {
            SPField field = null;

            try
            {
                field = item.Fields.GetField(key);
            }
            catch { }
            return field;
        }

        public static SPField GetField(SPList list, string key)
        {
            SPField field = null;

            try
            {
                field = list.Fields.GetField(key);
            }
            catch { }
            return field;
        }

        public static SPField GetField(SPList list, Guid key)
        {
            SPField field = null;

            try
            {
                field = list.Fields[key];
            }
            catch { }
            return field;
        }
        #endregion // GetField

        #endregion // Getters

        #region Setters
        public static void Set(SPListItem item, Guid key, object value)
        {
            try
            {
                item[key] = value;

                item.Web.AllowUnsafeUpdates = true;
                item.SystemUpdate(false);
                item.Web.AllowUnsafeUpdates = false;
            }
            catch { }
        }

        public static void Set(SPListItem item, string key, object value)
        {
            try
            {
                item[key] = value;

                item.Web.AllowUnsafeUpdates = true;
                item.SystemUpdate(false);
                item.Web.AllowUnsafeUpdates = false;
            }
            catch { }
        }

        public static void SetBoolean(SPListItem item, Guid key, bool value)
        {
            if (value)
                Set(item, key, "1");
            else
                Set(item, key, "0");
        }

        public static void SetBoolean(SPListItem item, string key, bool value)
        {
            if (value)
                Set(item, key, "1");
            else
                Set(item, key, "0");
        }
        #endregion // Setters

        #region Parsers
        // This should go
        public static object Parse(object value, object defaultValue)
        {
            if (value != null && value.GetType() == typeof(string))
            {
                if (defaultValue.GetType() == typeof(bool))
                {
                    return Helper.GetBooleanOrDefault(value.ToString(), (bool) defaultValue);
                }
                else if (defaultValue.GetType() == typeof(int))
                {
                    return Helper.GetIntegerOrDefault(value.ToString(), (int)defaultValue);
                }
                else if (defaultValue.GetType().IsEnum)
                {
                    return Enum.Parse(defaultValue.GetType(), value.ToString());
                }
                return value.ToString();
            }
            return null;
        }
        #endregion // Parsers
    }
}